var app = getApp(), systemInfo = wx.getSystemInfoSync();

Page({
    data: {
        navH: 0,
        dateObj: !1,
        chatList: [],
        plusH: 37,
        pageHeight: systemInfo.windowHeight,
        keyword: "",
        his_key: [],
        is_loading: !1
    },
    onLoad: function(a) {
        this.setData({
            navH: app.globalData.navHeight,
            app_name: app.globalData.setting.name,
            hot_skey: app.globalData.setting.hot_skey.split("|")
        }), this.data.dateObj = new app.util.date();
    },
    setSearchKey: function(a) {
        this.data.keyword = a.detail.value;
    },
    aiSearch: function(a) {
        if (void 0 !== a && (void 0 !== a.detail.value ? (t = a.detail.value, console.log(t)) : void 0 !== a.currentTarget.dataset.key && (t = a.currentTarget.dataset.key)), 
        !t) {
            if (!this.data.keyword) return;
            var t = this.data.keyword;
        }
        this.data.chatList.push({
            from: "user",
            type: "text",
            name: app.globalData.userInfo.nickName,
            avatar: app.globalData.userInfo.avatar,
            content: t,
            time_str: this.data.dateObj.dateToStr("MM-dd HH:mm")
        });
        var e = this;
        this.setData({
            chatList: this.data.chatList,
            keyword: ""
        }, function() {
            e.pageScrollToBottom();
        }), this.getAiSearch(t);
    },
    toGoodsPage: function(a) {
        a = a.currentTarget.dataset.goods_id, wx.navigateTo({
            url: "../goods/goods?goods_id=" + a
        });
    },
    getAiSearch: function(o) {
        var a, t, i = this, s = i.inArray(o, i.data.his_key);
        !1 === s ? (t = {
            keyword: o,
            page: 1
        }, i.data.his_key.push(t)) : (a = i.data.his_key.slice(s, s + 1), t = {
            keyword: o,
            page: parseInt(a[0].page) + 1
        }, i.data.his_key.splice(s, 1, t)), app.util.request({
            url: "entry/wxapp/getAiSearch",
            data: t,
            success: function(a) {
                if ("need" != a.data.message) {
                    var t = a.data.data.slice(0, 2);
                    if (t.length < 1) i.againSearch(o, s); else {
                        for (var e in t) i.data.chatList.push({
                            from: "official",
                            type: "goods",
                            name: app.globalData.setting.name,
                            avatar: app.globalData.setting.logo,
                            goods: {
                                title: t[e].goods_name,
                                price: (t[e].min_group_price - t[e].coupon_discount) / 100,
                                org_price: t[e].min_group_price / 100,
                                coupon: t[e].coupon_discount / 100,
                                img: t[e].goods_thumbnail_url,
                                id: t[e].goods_id,
                                store_logo: app.globalData.setting.logo,
                                store_name: app.globalData.setting.name
                            },
                            time_str: i.data.dateObj.dateToStr("MM-dd HH:mm")
                        });
                        i.setData({
                            chatList: i.data.chatList
                        }, function() {
                            i.pageScrollToBottom();
                        });
                    }
                } else wx.showModal({
                    title: "系统信息",
                    content: "需要先去拼多多授权才能搜索:)",
                    showCancel: !1,
                    confirmColor: "#f43f6c",
                    confirmText: "去授权",
                    complete: function() {
                        wx.navigateToMiniProgram({
                            appId: a.data.data.app_id,
                            path: a.data.data.page_path,
                            success: function(a) {
                                console.log("ok"), wx.navigateBack();
                            }
                        });
                    }
                });
            },
            fail: function() {
                i.againSearch(o, s);
            }
        });
    },
    againSearch: function(t, e) {
        var o = this;
        wx.showModal({
            title: "系统提示",
            content: "亲亲~~，我打了个盹，没有为您找到关于“" + t + "”的商品，您可以再试一次！！！",
            confirmText: "再来一次",
            cancelColor: "#cccccc",
            confirmColor: "#f43f6c",
            showCancel: !0,
            cancelText: "算了",
            success: function(a) {
                a.confirm && (o.data.keyword = t, o.data.his_key.splice(e, 1), o.aiSearch(), console.log("用户点击确定"));
            }
        });
    },
    inArray: function(a, t) {
        for (var e = 0, o = t.length; e < o; e++) if (a == t[e].keyword) return e;
        return !1;
    },
    onShow: function() {
        this.data.chatList.push({
            from: "official",
            type: "text",
            name: app.globalData.setting.name,
            avatar: app.globalData.setting.logo,
            content: "欢迎光临，请输入你想搜索的商品，我们将为您挑选最优惠的，发送给您。",
            time_str: this.data.dateObj.dateToStr("MM-dd HH:mm")
        });
        var a = this;
        this.setData({
            chatList: this.data.chatList
        }, function() {
            a.pageScrollToBottom();
        });
    },
    navBack: function() {
        wx.navigateBack();
    },
    toIndex: function() {
        wx.switchTab({
            url: "../index/index"
        });
    },
    pageScrollToBottom: function() {
        var a = 999 * this.data.chatList.length;
        this.setData({
            scrollTopVal: a
        });
    },
    onShareAppMessage: function() {
        var a = app.globalData.setting.share_title || "购物省钱，分享赚钱", t = app.globalData.setting.share_pic;
        return {
            title: a,
            path: "/first_duoduoke/pages/index/index" + (void 0 !== app.globalData.userInfo.uid ? "?from_uid=" + app.globalData.userInfo.uid + "&from_act=share" : ""),
            imageUrl: t,
            success: function(a) {
                "shareAppMessage:ok" == a.errMsg ? wx.showToast({
                    title: "分享成功",
                    icon: "success"
                }) : wx.showToast({
                    title: "分享失败",
                    icon: "none"
                });
            },
            fail: function(a) {
                "shareAppMessage:fail cancel" == a.errMsg || a.errMsg, wx.showToast({
                    title: "分享失败",
                    icon: "none"
                });
            }
        };
    }
});